//
//  SCAlertHUD.m
//  SCFoundationKit
//
//  Created by Chao Sheng on 2023/10/31.
//

#import "SCAlertHUD.h"
#import "SCBasicKit.h"

@implementation SCAlertHUD

+ (instancetype)share {
    static SCAlertHUD * manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[SCAlertHUD alloc] init];
        manager.delayDuration = 1.5f;
    });
    return manager;
}

- (void)showLoadingText:(NSString *)text {
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].sc_keyWindow animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        
        UIView *customView = [[UIView alloc] init];
        UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        if (@available(iOS 13.0, *)) {
            activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleMedium;
        } else {
            activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        }
        activityView.color = UIColor.whiteColor;
        activityView.hidesWhenStopped = true;
        [activityView startAnimating];
        [customView addSubview:activityView];
        [activityView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(@0);
            make.bottom.equalTo(@-5);
            make.width.height.equalTo(@20);
        }];
        
        hud.customView = customView;
        hud.detailsLabel.text = text;
        hud.detailsLabel.font = [UIFont systemFontOfSize:14];
        hud.bezelView.blurEffectStyle =  UIBlurEffectStyleDark;
        hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.8];
        hud.contentColor = [UIColor whiteColor];
        
    });
}

- (void)showSuccessWithMessage:(NSString *)message {
    [self showHudWithMessage:message Image:[UIImage imageNamed:@"sc-hudsuccess"]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(self.delayDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].sc_keyWindow animated:YES];
    });
}

- (void)showFailureWithMessage:(NSString *)message {
    [self showHudWithMessage:message Image:[UIImage imageNamed:@"sc-huderror"]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(self.delayDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].sc_keyWindow animated:YES];
    });
}

- (void)showWarnWithMessage:(NSString *)message {
    [self showHudWithMessage:message Image:[UIImage imageNamed:@"sc-hudwarning"]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(self.delayDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].sc_keyWindow animated:YES];
    });
}

- (void)showMessage:(NSString *)message {
    [self showHudWithMessage:message Image:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(self.delayDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].sc_keyWindow animated:YES];
    });
}

- (void)showHudWithMessage:(NSString *)message Image:(UIImage * __nullable)image {
    dispatch_async(dispatch_get_main_queue(), ^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].sc_keyWindow animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        
        if(image) {
            UIView *customView = [[UIView alloc] init];
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.image = image;
            CGSize size = image.size;
            [customView addSubview:imageView];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.right.equalTo(@0);
                make.bottom.equalTo(@-5);
                make.size.mas_equalTo(size);
            }];
            hud.customView = customView;
        } else {
            hud.customView = nil;
        }
        hud.detailsLabel.text = message;
        hud.detailsLabel.font = [UIFont systemFontOfSize:14];
        hud.bezelView.blurEffectStyle =  UIBlurEffectStyleDark;
        hud.bezelView.color = [[UIColor blackColor] colorWithAlphaComponent:0.8];
        hud.contentColor = [UIColor whiteColor];
        
    });

}

- (void)hide {
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].sc_keyWindow animated:YES];
    });
}

@end
